package de.graffiti.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.graffiti.beans.DBConnection;
import de.graffiti.interfaces.Content;
import de.graffiti.interfaces.WallpaperService;

/**
 * Servlet implementation class TranscodingServlet
 */
public class ContentServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ContentServlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String type = request.getParameter("type");

		byte[] image = getImage(type, request, response);

		ServletOutputStream os = response.getOutputStream();
		os.write(image);
		os.close();

	}

	private byte[] getImage(String type, HttpServletRequest request, HttpServletResponse response)
	{
		int wallpaperid = Integer.parseInt(request.getParameter("contentid"));

		DBConnection db = new DBConnection();
		WallpaperService wpservice = (WallpaperService) db.getService("WallpaperServiceBean/remote", WallpaperService.class);

		Content con = wpservice.getContent(wallpaperid);
		response.setContentType(con.getContenttype());

		return con.getData();

	}

}
